package com.example.foods.controller;

import com.example.foods.domain.AjaxResult;
import com.example.foods.service.OrderIService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Resource
    private OrderIService orderIService;

    //查询所有订单
    @GetMapping("/list")
    public AjaxResult list() {
        return AjaxResult.ok(this.orderIService.list());
    }

    //下单
    @GetMapping("/onOrder/{userId}")
    public AjaxResult onOrder(@PathVariable("userId") Integer userId) {
        orderIService.onOrder(userId);
        return AjaxResult.ok(true);
    }

    @GetMapping("/send/{orderId}")
    public void send(@PathVariable("orderId") Integer orderId, HttpServletResponse response) throws IOException {
        orderIService.send(orderId);
        //重定向
        String redirectUrl = "http://localhost:8080/orders/orders_list.html";
        response.sendRedirect(redirectUrl);
    }
}
